package userctr

import (
	"auusoft/AuuWiki/Models"
	"auusoft/AuuWiki/controllers"
	// "fmt"
)

type ChangePasswordController struct {
	controllers.RootController
}

func (this *ChangePasswordController) Post() {
	username := this.CheckSession()

	this.Ctx.Request.ParseForm()
	oldpassword := this.Ctx.Request.Form.Get("oldpassword")
	newpassword := this.Ctx.Request.Form.Get("newpassword")
	newpassword2 := this.Ctx.Request.Form.Get("newpassword2")

	// fmt.Println(oldpassword)
	// fmt.Println(newpassword2)
	// fmt.Println(newpassword)
	if newpassword != newpassword2 {
		this.Ctx.WriteString("错误:两次输入的新密码不一致")
		return
	}

	users, _ := models.GetUserInfoByUsername(username.(string), 0, -1)

	if len(users) <= 0 {
		this.Ctx.WriteString("错误:没有找到当前用户信息")
		return
	}

	if users[0].Password != oldpassword {
		this.Ctx.WriteString("错误:旧密码错误")
		return
	}

	users[0].Password = newpassword

	models.UpdateUserInfo(users[0])

	//客户端用ajax接收数据，所以随便发点啥回去就行了
	this.Ctx.WriteString("密码修改成功")
	//this.Ctx.Redirect(302, "/userctr/userlist")
}
